import Vue from 'vue'
import Router from 'vue-router'
import Constants from '@/const'
import store from '../store/index'
import routerName from "@/const/RouterName"
import BalaBala from "@/methods/BalaBala"
import Token from "@/methods/Token"

Vue.use(Router)

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return {x: 0, y: 0}
    }
  },
  routes: [
    {
      path: '/login',
      name: Constants.routerName.login,
      // @ts-ignore
      component: () => import('@/views/login/login.vue'),
      meta: {keepAlive: true}
    },
    {
      path: '/chat',
      name: Constants.routerName.chat,
      component: () => import('@/views/chat/chat.vue'),
      meta: {keepAlive: false}
    },
    {
      path: '/match',
      name: Constants.routerName.match,
      // @ts-ignore
      component: () => import('@/views/match/match'),
      meta: {keepAlive: true}
    },
    {
      path: '/',
      name: Constants.routerName.talk,
      // @ts-ignore
      component: () => import('@/views/talk/talk'),
      meta: {keepAlive: false}
    },
    {
      path: '/user/mine',
      name: Constants.routerName.mine,
      // @ts-ignore
      component: () => import('@/views/user/mine/mine'),
      meta: {keepAlive: true}
    },
    {
      path: '/talkAdd',
      name: Constants.routerName.talkAdd,
      component: () => import('@/views/talk/talkAdd/talkAdd.vue'),
      props: true,
      meta: {keepAlive: false}
    },
    {
      path: '/talkDetail/:talkId',
      name: Constants.routerName.talkDetail,
      component: () => import('@/views/talk/talkDetail/talkDetail.vue'),
      props: true,
      meta: {keepAlive: false}
    },
    {
      path: '/message/:chatId',
      name: Constants.routerName.message,
      component: () => import('@/views/message/message.vue'),
      props: true,
      meta: {keepAlive: false}
    },
    {
      path: '/user/authentication',
      name: Constants.routerName.authentication,
      // @ts-ignore
      component: () => import('@/views/user/authentication/authentication'),
      meta: {keepAlive: false}
    },
    {
      path: '/user/vip',
      name: Constants.routerName.vip,
      // @ts-ignore
      component: () => import('@/views/user/vip/vip'),
      meta: {keepAlive: false}
    },
    {
      path: '/notify',
      name: Constants.routerName.notify,
      // @ts-ignore
      component: () => import('@/views/notify/notify'),
      meta: {keepAlive: false}
    },
    {
      path: '/report',
      name: Constants.routerName.report,
      // @ts-ignore
      component: () => import('@/views/report/report'),
      meta: {keepAlive: false}
    },
    {
      path: '/*',
      redirect: '/'
    },
  ]
})

router.beforeEach((to, from, next) => {
  store.commit('talk/inputContentBlur')
  //如果有token
  if (Token.get()) {
    //没用户则去后台获取
    // @ts-ignore
    if (!store.state.user.user) {
      store.dispatch('user/getUserInfo').then(() => {
        store.dispatch('notify/queryUnreadNotifies')
        store.dispatch('chat/getChats').then(() => {
          next()
        })
      })
    } else {
      next()
    }
    //没登录过
  } else {
    //只允许进入 匹配列表页和登录页
    if (![routerName.match, routerName.talk, routerName.login].includes(to.name)) {
      BalaBala.unLoginMessage()
    } else {
      next()
    }
  }
})

export default router
